package com.lhd.app.utils;

import com.lhd.app.bean.PetTypeLabel;
import com.lhd.app.bean.UserBehavior;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

// 数据导出工具类
public class DataExporter {
    
    // 导出原始数据到CSV
    public static void exportRawData(List<UserBehavior> behaviors, String filename) {
        File dataDir = new File("data");
        if (!dataDir.exists()) {
            dataDir.mkdirs();
        }
        
        try (BufferedWriter writer = new BufferedWriter(new FileWriter("data/" + filename))) {
            // 写入表头
            writer.write("user_id,behavior_type,category1,category2,category3,timestamp,attributes\n");
            
            // 写入数据
            for (UserBehavior behavior : behaviors) {
                String record = String.format("%s,%s,%s,%s,%s,%d,%s%n",
                    behavior.getUserId(),
                    behavior.getBehaviorType(),
                    behavior.getCategory1(),
                    behavior.getCategory2(),
                    behavior.getCategory3(),
                    behavior.getTimestamp(),
                    formatAttributes(behavior.getItemAttributes()));
                
                writer.write(record);
            }
            
            System.out.println("Exported " + behaviors.size() + " raw records to data/" + filename);
        } catch (IOException e) {
            System.err.println("Failed to export raw data: " + e.getMessage());
        }
    }
    
    // 导出处理后的数据
    public static void exportProcessedData(List<PetTypeLabel> labels, String filename) {
        File dataDir = new File("data");
        if (!dataDir.exists()) {
            dataDir.mkdirs();
        }
        
        try (BufferedWriter writer = new BufferedWriter(new FileWriter("data/" + filename))) {
            // 写入表头
            writer.write("user_id,pet_labels,pet_scores,update_time\n");
            
            // 写入数据
            for (PetTypeLabel label : labels) {
                String record = String.format("%s,%s,%s,%d%n",
                    label.getUserId(),
                    String.join("|", label.getPetLabels()),
                    formatScores(label.getPetScores()),
                    label.getUpdateTime());
                
                writer.write(record);
            }
            
            System.out.println("Exported " + labels.size() + " processed records to data/" + filename);
        } catch (IOException e) {
            System.err.println("Failed to export processed data: " + e.getMessage());
        }
    }
    
    private static String formatAttributes(Map<String, String> attributes) {
        if (attributes == null || attributes.isEmpty()) {
            return "";
        }
        return attributes.entrySet().stream()
            .map(entry -> entry.getKey() + "=" + entry.getValue())
            .collect(Collectors.joining(";"));
    }
    
    private static String formatScores(Map<String, Double> scores) {
        return scores.entrySet().stream()
            .map(entry -> entry.getKey() + ":" + String.format("%.2f", entry.getValue()))
            .collect(Collectors.joining("|"));
    }
}